{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "using ExprOptimization"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "ExprRules.Grammar(Any[:x, :(Real * Real), :(Real + Real), :(Real - Real), 1.0, 2.0, 3.0, 4.0, 5.0], Symbol[:Real, :Real, :Real, :Real, :Real, :Real, :Real, :Real, :Real], Bool[true, false, false, false, true, true, true, true, true], Bool[false, false, false, false, false, false, false, false, false], Dict(:Real=>[1, 2, 3, 4, 5, 6, 7, 8, 9]), Array{Symbol,1}[Symbol[], Symbol[:Real, :Real], Symbol[:Real, :Real], Symbol[:Real, :Real], Symbol[], Symbol[], Symbol[], Symbol[], Symbol[]])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grammar = @grammar begin\n",
    "    Real = x\n",
    "    Real = Real * Real\n",
    "    Real = Real + Real\n",
    "    Real = Real - Real\n",
    "    Real = |(1.0:5.0)\n",
    "end"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "loss (generic function with 1 method)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ground_truth(x) = 3x^2 + 2x + 1\n",
    "function loss(tree::RuleNode, grammar::Grammar)\n",
    "    ex = get_executable(tree, grammar)\n",
    "    los = 0.0\n",
    "    global x\n",
    "    for x = -5.0:0.1:5.0\n",
    "        los += abs2(eval(ex) - ground_truth(x))\n",
    "    end\n",
    "    los\n",
    "end"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "ExprOptimization.GrammaticalEvolution.GrammaticalEvolutionParams(10, 10, 5, 6, [0.2, 0.4, 0.4], ExprOptimization.GrammaticalEvolution.TournamentSelection(2), ExprOptimization.GrammaticalEvolution.MultiMutate(ExprOptimization.GrammaticalEvolution.MutationMethod[ExprOptimization.GrammaticalEvolution.RandomMutation(0.1), ExprOptimization.GrammaticalEvolution.GeneDuplication(), ExprOptimization.GrammaticalEvolution.GenePruning(0.1, ExprRules.Grammar(Any[:x, :(Real * Real), :(Real + Real), :(Real - Real), 1.0, 2.0, 3.0, 4.0, 5.0], Symbol[:Real, :Real, :Real, :Real, :Real, :Real, :Real, :Real, :Real], Bool[true, false, false, false, true, true, true, true, true], Bool[false, false, false, false, false, false, false, false, false], Dict(:Real=>[1, 2, 3, 4, 5, 6, 7, 8, 9]), Array{Symbol,1}[Symbol[], Symbol[:Real, :Real], Symbol[:Real, :Real], Symbol[:Real, :Real], Symbol[], Symbol[], Symbol[], Symbol[], Symbol[]]), :Real)]))"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "srand(0)\n",
    "p = GrammaticalEvolutionParams(grammar,:Real,10, 10, 5, 6, 0.2, 0.4, 0.4; select_method=GrammaticalEvolution.TournamentSelection())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10-element Array{Array{Int64,1},1}:\n",
       " [-5709378026784038018, -4638217506560872160, 2412787040955850733, -6755966082412089502, -6661740532992639830]\n",
       " [-651199173612846156, 4109948044794495697, -3302889169951444445, 1729965902600649201, -6862518068746397061]  \n",
       " [613121453027881482, 2055429552382091182, -6192964121268396252, 4404489758546396735, -4258652183208957536]   \n",
       " [539243902679897220, -4083976551409006831, -7241691602384556895, -758093089873472298, -2289421348640196976]  \n",
       " [-6081565160420883285, 2458414197319666803, 5672049372335834747, -9210495900809319837, -1032048120210058855] \n",
       " [2685370321605084886, -3702360005862138301, 8603170902964801110, 2744240855864043070, 2855546127016599912]   \n",
       " [-807930431977090474, 2932880398911546040, 8954361803976984784, -6678951359272536859, 4793069196214468516]   \n",
       " [1373126756106904216, -1898737214698672848, -3356314334213227875, 10354336246505119, -9019357450546061931]   \n",
       " [-977476258477762621, -1071934068849374472, 563055167799650783, 6482647521917630674, 9165324684192676362]    \n",
       " [6171183958118147984, -2535981851306386712, 2590065991392420216, 5802933594092514868, 1207099931249803939]   "
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "srand(2)\n",
    "using ExprOptimization.GrammaticalEvolution\n",
    "pop = GrammaticalEvolution.initialize(p.pop_size, p.gene_length)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "ExprOptimization.GrammaticalEvolution.DecodedExpression(4{4{8,5}1}, 5)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "GrammaticalEvolution.decode(pop[1], grammar, :Real)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10-element Array{ExprRules.RuleNode,1}:\n",
       " 4{4{8,5}1}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               \n",
       " 9,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       \n",
       " 3{1,6}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   \n",
       " 6,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       \n",
       " 3{6,8}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   \n",
       " 1,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       \n",
       " 8,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       \n",
       " 7,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       \n",
       " 2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2{3{2{7,9}2}3}}2}}7}}9}}2}}3}}2}}7}}9}}2}}3}}2}}7}}9}}2}}3}}2}}7}}9}}2}}3}}2}}7}}9}}2}}3}}2}}7}}9}}2}}3}}2}}7}}9}}2}}3}}2}}7}}9}}2}}3}}2}}7}}9}}2}}3}}2}}7}}9}}2}}3}}2}}7}}9}}2}}3}}2}}7}}9}}2}}3}}2}}7}}9}}2}}3}}2}}7}}9}}2}}3}}2}}7}}9}}2}}3}}2}}7}}9}}2}}3}}2}}7}}9}}2}}3}}2}}7}}9}}2}}3}}2}}7}}9}}2}}3}}2}}7}}9}}2}}3}}2}}7}}9}}2}}3}}2}}7}}9}}2}}3}}2}}7}}9}}2}}3}}2}}7}}9}}2}}3}}2}}7}}9}}2}}3}}2}}7}}9}}2}}3}}2}}7}}9}}2}}3}}2}}7}}9}}2}}3}}2}}7}}9}}2}}3}}2}}7}}9}}2}}3}}2}}7}}9}}2}}3}}2}}7}}9}}2}}3}}2}}7}}9}}2}}3}}2}}7}}9}}2}}3}}2}}7}}9}}2}}3}}2}}7}}9}}2}}3}}2}}7}}9}}2}}3}}2}}7}}9}}2}}3}}2}}7}}9}}2}}3}}2}}7}}9}}2}\n",
       " 2{7,3{1,7}}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              "
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "trees=[GrammaticalEvolution.decode(i, grammar, :Real).node for i in pop]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2{7,3{1,7}}, 84314.49699999999)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "losses = zeros(p.pop_size)\n",
    "best_tree, best_loss = GrammaticalEvolution.evaluate!(p, grammar, :Real, loss, pop, losses, RuleNode(0), Inf)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10-element Array{Float64,1}:\n",
       "  84314.5      \n",
       "  98404.0      \n",
       " 102646.0      \n",
       " 107090.0      \n",
       " 111736.0      \n",
       "      1.14008e5\n",
       "      1.16028e5\n",
       " 116584.0      \n",
       "      1.2431e5 \n",
       "    Inf        "
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "losses"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n",
       "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"64.852pt\" height=\"69.082pt\" viewBox=\"0 0 64.852 69.082\" version=\"1.1\">\n",
       "<defs>\n",
       "<g>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1506981941267957-0-0\">\n",
       "<path style=\"stroke:none;\" d=\"\"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1506981941267957-0-1\">\n",
       "<path style=\"stroke:none;\" d=\"M 4.546875 -2.140625 C 4.546875 -2.296875 4.421875 -2.390625 4.40625 -2.40625 C 4.15625 -2.578125 3.484375 -2.90625 3.21875 -3.046875 L 4.296875 -3.625 C 4.421875 -3.703125 4.546875 -3.765625 4.546875 -3.953125 C 4.546875 -4 4.546875 -4.28125 4.140625 -4.28125 L 2.90625 -3.5625 C 2.9375 -3.828125 2.9375 -4.484375 2.9375 -4.78125 C 2.9375 -4.859375 2.9375 -5.1875 2.609375 -5.1875 C 2.28125 -5.1875 2.28125 -4.859375 2.28125 -4.78125 C 2.28125 -4.484375 2.296875 -3.828125 2.3125 -3.5625 L 1.234375 -4.203125 C 1.09375 -4.28125 1.078125 -4.28125 1 -4.28125 C 0.8125 -4.28125 0.671875 -4.109375 0.671875 -3.953125 C 0.671875 -3.765625 0.78125 -3.703125 0.921875 -3.640625 L 2 -3.046875 L 0.921875 -2.46875 C 0.8125 -2.390625 0.671875 -2.328125 0.671875 -2.140625 C 0.671875 -2.09375 0.671875 -1.796875 1.078125 -1.796875 L 2.3125 -2.515625 C 2.296875 -2.265625 2.28125 -1.609375 2.28125 -1.3125 C 2.28125 -1.21875 2.28125 -0.890625 2.609375 -0.890625 C 2.9375 -0.890625 2.9375 -1.21875 2.9375 -1.3125 C 2.9375 -1.609375 2.9375 -2.265625 2.90625 -2.515625 C 3.515625 -2.1875 3.15625 -2.375 3.84375 -1.96875 C 4.109375 -1.796875 4.140625 -1.796875 4.21875 -1.796875 C 4.421875 -1.796875 4.546875 -1.984375 4.546875 -2.140625 Z M 4.546875 -2.140625 \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1506981941267957-0-2\">\n",
       "<path style=\"stroke:none;\" d=\"M 4.78125 -1.734375 C 4.78125 -2.4375 4.3125 -3.03125 3.65625 -3.328125 C 4.21875 -3.6875 4.5 -4.25 4.5 -4.796875 C 4.5 -5.53125 3.765625 -6.203125 2.625 -6.203125 C 1.421875 -6.203125 0.734375 -5.71875 0.734375 -5.03125 C 0.734375 -4.703125 0.984375 -4.5625 1.171875 -4.5625 C 1.390625 -4.5625 1.609375 -4.734375 1.609375 -5.015625 C 1.609375 -5.15625 1.5625 -5.25 1.53125 -5.28125 C 1.828125 -5.59375 2.546875 -5.59375 2.625 -5.59375 C 3.3125 -5.59375 3.8125 -5.234375 3.8125 -4.78125 C 3.8125 -4.484375 3.65625 -4.140625 3.390625 -3.921875 C 3.078125 -3.65625 2.828125 -3.640625 2.46875 -3.625 C 1.890625 -3.578125 1.75 -3.578125 1.75 -3.296875 C 1.75 -2.984375 1.984375 -2.984375 2.140625 -2.984375 L 2.609375 -2.984375 C 3.59375 -2.984375 4.09375 -2.3125 4.09375 -1.734375 C 4.09375 -1.125 3.53125 -0.5 2.625 -0.5 C 2.234375 -0.5 1.46875 -0.609375 1.203125 -1.078125 C 1.25 -1.125 1.328125 -1.1875 1.328125 -1.390625 C 1.328125 -1.625 1.140625 -1.828125 0.890625 -1.828125 C 0.65625 -1.828125 0.4375 -1.671875 0.4375 -1.359375 C 0.4375 -0.46875 1.40625 0.109375 2.625 0.109375 C 3.9375 0.109375 4.78125 -0.8125 4.78125 -1.734375 Z M 4.78125 -1.734375 \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1506981941267957-0-3\">\n",
       "<path style=\"stroke:none;\" d=\"M 3.234375 -0.625 C 3.234375 -0.984375 2.9375 -1.25 2.625 -1.25 C 2.25 -1.25 2 -0.9375 2 -0.625 C 2 -0.265625 2.296875 0 2.609375 0 C 2.984375 0 3.234375 -0.3125 3.234375 -0.625 Z M 3.234375 -0.625 \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1506981941267957-0-4\">\n",
       "<path style=\"stroke:none;\" d=\"M 4.71875 -3.046875 C 4.71875 -4.890625 3.703125 -6.203125 2.609375 -6.203125 C 1.5 -6.203125 0.5 -4.859375 0.5 -3.046875 C 0.5 -1.203125 1.515625 0.109375 2.609375 0.109375 C 3.734375 0.109375 4.71875 -1.21875 4.71875 -3.046875 Z M 4.03125 -3.15625 C 4.03125 -1.671875 3.390625 -0.5 2.609375 -0.5 C 1.828125 -0.5 1.1875 -1.671875 1.1875 -3.15625 C 1.1875 -4.609375 1.875 -5.59375 2.609375 -5.59375 C 3.34375 -5.59375 4.03125 -4.609375 4.03125 -3.15625 Z M 4.03125 -3.15625 \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1506981941267957-0-5\">\n",
       "<path style=\"stroke:none;\" d=\"M 4.84375 -3.046875 C 4.84375 -3.40625 4.5 -3.40625 4.375 -3.40625 L 2.953125 -3.40625 L 2.953125 -4.828125 C 2.953125 -4.9375 2.953125 -5.296875 2.609375 -5.296875 C 2.265625 -5.296875 2.265625 -4.9375 2.265625 -4.828125 L 2.265625 -3.40625 L 0.84375 -3.40625 C 0.734375 -3.40625 0.375 -3.40625 0.375 -3.046875 C 0.375 -2.703125 0.734375 -2.703125 0.84375 -2.703125 L 2.265625 -2.703125 L 2.265625 -1.28125 C 2.265625 -1.15625 2.265625 -0.8125 2.609375 -0.8125 C 2.953125 -0.8125 2.953125 -1.15625 2.953125 -1.28125 L 2.953125 -2.703125 L 4.375 -2.703125 C 4.5 -2.703125 4.84375 -2.703125 4.84375 -3.046875 Z M 4.84375 -3.046875 \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1506981941267957-0-6\">\n",
       "<path style=\"stroke:none;\" d=\"M 4.9375 -0.296875 C 4.9375 -0.609375 4.6875 -0.609375 4.53125 -0.609375 L 4.140625 -0.609375 L 2.859375 -2.21875 L 4 -3.6875 L 4.390625 -3.6875 C 4.53125 -3.6875 4.8125 -3.6875 4.8125 -3.984375 C 4.8125 -4.296875 4.546875 -4.296875 4.390625 -4.296875 L 3.234375 -4.296875 C 3.078125 -4.296875 2.828125 -4.296875 2.828125 -4 C 2.828125 -3.6875 3.046875 -3.6875 3.3125 -3.6875 L 2.578125 -2.6875 L 1.828125 -3.6875 C 2.078125 -3.6875 2.296875 -3.6875 2.296875 -4 C 2.296875 -4.296875 2.046875 -4.296875 1.90625 -4.296875 L 0.734375 -4.296875 C 0.59375 -4.296875 0.328125 -4.296875 0.328125 -3.984375 C 0.328125 -3.6875 0.59375 -3.6875 0.734375 -3.6875 L 1.140625 -3.6875 L 2.3125 -2.21875 L 1.078125 -0.609375 L 0.671875 -0.609375 C 0.53125 -0.609375 0.265625 -0.609375 0.265625 -0.296875 C 0.265625 0 0.53125 0 0.671875 0 L 1.84375 0 C 2 0 2.25 0 2.25 -0.296875 C 2.25 -0.609375 2.03125 -0.609375 1.71875 -0.609375 L 2.578125 -1.828125 L 3.46875 -0.609375 C 3.1875 -0.609375 2.96875 -0.609375 2.96875 -0.296875 C 2.96875 0 3.21875 0 3.375 0 L 4.53125 0 C 4.671875 0 4.9375 0 4.9375 -0.296875 Z M 4.9375 -0.296875 \"/>\n",
       "</symbol>\n",
       "</g>\n",
       "</defs>\n",
       "<g id=\"surface1\">\n",
       "<path style=\"fill:none;stroke-width:0.3985;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -3.05775 -6.11625 L -10.631969 -21.264688 \" transform=\"matrix(1,0,0,-1,25.339,5.915)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.31879;stroke-linecap:round;stroke-linejoin:round;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -1.194205 1.595227 C -1.096361 0.997783 -0.00101897 0.0998963 0.297707 0.00032998 C -0.00101358 -0.100999 -1.094561 -0.997198 -1.194119 -1.5929 \" transform=\"matrix(-0.44724,0.89442,0.89442,0.44724,14.70707,27.17889)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.3985;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M 3.059437 -6.11625 L 10.887562 -21.7725 \" transform=\"matrix(1,0,0,-1,25.339,5.915)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.31879;stroke-linecap:round;stroke-linejoin:round;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -1.193663 1.594409 C -1.095852 0.995213 -0.00230495 0.0990149 0.298162 0.00117935 C -0.000563331 -0.098387 -1.095905 -0.996274 -1.193749 -1.593717 \" transform=\"matrix(0.44724,0.89442,0.89442,-0.44724,36.22497,27.68697)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.3985;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M 11.090687 -34.510781 L 3.039906 -50.61625 \" transform=\"matrix(1,0,0,-1,25.339,5.915)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.31879;stroke-linecap:round;stroke-linejoin:round;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -1.196573 1.594952 C -1.097399 0.996396 -0.00121626 0.100294 0.29806 -0.00236792 C -0.00121804 -0.0998044 -1.097417 -0.995887 -1.194861 -1.592701 \" transform=\"matrix(-0.44899,0.898,0.898,0.44899,28.37814,56.53028)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.3985;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M 17.25475 -34.510781 L 24.805531 -49.608438 \" transform=\"matrix(1,0,0,-1,25.339,5.915)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.31879;stroke-linecap:round;stroke-linejoin:round;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -1.195451 1.594989 C -1.094532 0.996437 0.00165367 0.100348 0.29919 -0.000570713 C 0.00165367 -0.0997494 -1.094532 -0.995838 -1.193711 -1.59439 \" transform=\"matrix(0.449,0.898,0.898,-0.449,50.14352,55.52404)\"/>\n",
       "<g style=\"fill:rgb(0%,0%,0%);fill-opacity:1;\">\n",
       "  <use xlink:href=\"#glyph-1506981941267957-0-1\" x=\"22.724\" y=\"8.511\"/>\n",
       "</g>\n",
       "<g style=\"fill:rgb(0%,0%,0%);fill-opacity:1;\">\n",
       "  <use xlink:href=\"#glyph-1506981941267957-0-2\" x=\"3.321\" y=\"37.305\"/>\n",
       "  <use xlink:href=\"#glyph-1506981941267957-0-3\" x=\"8.551386\" y=\"37.305\"/>\n",
       "  <use xlink:href=\"#glyph-1506981941267957-0-4\" x=\"13.781772\" y=\"37.305\"/>\n",
       "</g>\n",
       "<g style=\"fill:rgb(0%,0%,0%);fill-opacity:1;\">\n",
       "  <use xlink:href=\"#glyph-1506981941267957-0-5\" x=\"36.897\" y=\"36.907\"/>\n",
       "</g>\n",
       "<g style=\"fill:rgb(0%,0%,0%);fill-opacity:1;\">\n",
       "  <use xlink:href=\"#glyph-1506981941267957-0-6\" x=\"22.724\" y=\"64.755\"/>\n",
       "</g>\n",
       "<g style=\"fill:rgb(0%,0%,0%);fill-opacity:1;\">\n",
       "  <use xlink:href=\"#glyph-1506981941267957-0-2\" x=\"45.84\" y=\"65.652\"/>\n",
       "  <use xlink:href=\"#glyph-1506981941267957-0-3\" x=\"51.070386\" y=\"65.652\"/>\n",
       "  <use xlink:href=\"#glyph-1506981941267957-0-4\" x=\"56.300772\" y=\"65.652\"/>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n",
       "\n"
      ],
      "text/plain": [
       "TreeView.LabelledTree({5, 4} directed simple Int64 graph, Any[:*, 3.0, :+, :x, 3.0])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "display(best_tree, grammar)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       ":x"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "srand(0)\n",
    "ind1 = GrammaticalEvolution.select(p.select_method, pop, losses)\n",
    "node1 = GrammaticalEvolution.decode(ind1, grammar, :Real).node\n",
    "display(node1, grammar)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n",
       "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"60.131pt\" height=\"68.191pt\" viewBox=\"0 0 60.131 68.191\" version=\"1.1\">\n",
       "<defs>\n",
       "<g>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1506981941267956-0-0\">\n",
       "<path style=\"stroke:none;\" d=\"\"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1506981941267956-0-1\">\n",
       "<path style=\"stroke:none;\" d=\"M 4.671875 -3.046875 C 4.671875 -3.40625 4.3125 -3.40625 4.203125 -3.40625 L 1.03125 -3.40625 C 0.90625 -3.40625 0.5625 -3.40625 0.5625 -3.046875 C 0.5625 -2.703125 0.90625 -2.703125 1.03125 -2.703125 L 4.203125 -2.703125 C 4.3125 -2.703125 4.671875 -2.703125 4.671875 -3.046875 Z M 4.671875 -3.046875 \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1506981941267956-0-2\">\n",
       "<path style=\"stroke:none;\" d=\"M 4.9375 -1.984375 C 4.9375 -2.296875 4.6875 -2.296875 4.53125 -2.296875 L 3.765625 -2.296875 L 3.765625 -5.796875 C 3.765625 -6.125 3.6875 -6.203125 3.34375 -6.203125 L 3.078125 -6.203125 C 2.828125 -6.203125 2.78125 -6.203125 2.65625 -6 L 0.375 -2.4375 C 0.296875 -2.296875 0.296875 -2.28125 0.296875 -2.078125 C 0.296875 -1.75 0.375 -1.6875 0.6875 -1.6875 L 3.203125 -1.6875 L 3.203125 -0.609375 L 2.578125 -0.609375 C 2.421875 -0.609375 2.1875 -0.609375 2.1875 -0.296875 C 2.1875 0 2.4375 0 2.578125 0 L 4.375 0 C 4.53125 0 4.78125 0 4.78125 -0.296875 C 4.78125 -0.609375 4.53125 -0.609375 4.375 -0.609375 L 3.765625 -0.609375 L 3.765625 -1.6875 L 4.53125 -1.6875 C 4.6875 -1.6875 4.9375 -1.6875 4.9375 -1.984375 Z M 3.203125 -2.296875 L 0.984375 -2.296875 L 3.203125 -5.765625 Z M 3.203125 -2.296875 \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1506981941267956-0-3\">\n",
       "<path style=\"stroke:none;\" d=\"M 3.234375 -0.625 C 3.234375 -0.984375 2.9375 -1.25 2.625 -1.25 C 2.25 -1.25 2 -0.9375 2 -0.625 C 2 -0.265625 2.296875 0 2.609375 0 C 2.984375 0 3.234375 -0.3125 3.234375 -0.625 Z M 3.234375 -0.625 \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1506981941267956-0-4\">\n",
       "<path style=\"stroke:none;\" d=\"M 4.71875 -3.046875 C 4.71875 -4.890625 3.703125 -6.203125 2.609375 -6.203125 C 1.5 -6.203125 0.5 -4.859375 0.5 -3.046875 C 0.5 -1.203125 1.515625 0.109375 2.609375 0.109375 C 3.734375 0.109375 4.71875 -1.21875 4.71875 -3.046875 Z M 4.03125 -3.15625 C 4.03125 -1.671875 3.390625 -0.5 2.609375 -0.5 C 1.828125 -0.5 1.1875 -1.671875 1.1875 -3.15625 C 1.1875 -4.609375 1.875 -5.59375 2.609375 -5.59375 C 3.34375 -5.59375 4.03125 -4.609375 4.03125 -3.15625 Z M 4.03125 -3.15625 \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1506981941267956-0-5\">\n",
       "<path style=\"stroke:none;\" d=\"M 4.40625 -0.296875 C 4.40625 -0.609375 4.171875 -0.609375 4 -0.609375 L 3.09375 -0.609375 L 3.09375 -5.796875 C 3.09375 -5.953125 3.09375 -6.203125 2.796875 -6.203125 C 2.609375 -6.203125 2.546875 -6.078125 2.5 -5.96875 C 2.125 -5.109375 1.609375 -5 1.421875 -4.984375 C 1.25 -4.96875 1.046875 -4.953125 1.046875 -4.671875 C 1.046875 -4.421875 1.21875 -4.375 1.375 -4.375 C 1.5625 -4.375 1.96875 -4.4375 2.40625 -4.8125 L 2.40625 -0.609375 L 1.5 -0.609375 C 1.34375 -0.609375 1.109375 -0.609375 1.109375 -0.296875 C 1.109375 0 1.359375 0 1.5 0 L 4 0 C 4.15625 0 4.40625 0 4.40625 -0.296875 Z M 4.40625 -0.296875 \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1506981941267956-0-6\">\n",
       "<path style=\"stroke:none;\" d=\"M 4.9375 -0.296875 C 4.9375 -0.609375 4.6875 -0.609375 4.53125 -0.609375 L 4.140625 -0.609375 L 2.859375 -2.21875 L 4 -3.6875 L 4.390625 -3.6875 C 4.53125 -3.6875 4.8125 -3.6875 4.8125 -3.984375 C 4.8125 -4.296875 4.546875 -4.296875 4.390625 -4.296875 L 3.234375 -4.296875 C 3.078125 -4.296875 2.828125 -4.296875 2.828125 -4 C 2.828125 -3.6875 3.046875 -3.6875 3.3125 -3.6875 L 2.578125 -2.6875 L 1.828125 -3.6875 C 2.078125 -3.6875 2.296875 -3.6875 2.296875 -4 C 2.296875 -4.296875 2.046875 -4.296875 1.90625 -4.296875 L 0.734375 -4.296875 C 0.59375 -4.296875 0.328125 -4.296875 0.328125 -3.984375 C 0.328125 -3.6875 0.59375 -3.6875 0.734375 -3.6875 L 1.140625 -3.6875 L 2.3125 -2.21875 L 1.078125 -0.609375 L 0.671875 -0.609375 C 0.53125 -0.609375 0.265625 -0.609375 0.265625 -0.296875 C 0.265625 0 0.53125 0 0.671875 0 L 1.84375 0 C 2 0 2.25 0 2.25 -0.296875 C 2.25 -0.609375 2.03125 -0.609375 1.71875 -0.609375 L 2.578125 -1.828125 L 3.46875 -0.609375 C 3.1875 -0.609375 2.96875 -0.609375 2.96875 -0.296875 C 2.96875 0 3.21875 0 3.375 0 L 4.53125 0 C 4.671875 0 4.9375 0 4.9375 -0.296875 Z M 4.9375 -0.296875 \"/>\n",
       "</symbol>\n",
       "</g>\n",
       "</defs>\n",
       "<g id=\"surface1\">\n",
       "<path style=\"fill:none;stroke-width:0.3985;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -2.607938 -5.219281 L -11.357938 -22.719281 \" transform=\"matrix(1,0,0,-1,40.022,5.019)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.31879;stroke-linecap:round;stroke-linejoin:round;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -1.194716 1.596441 C -1.095135 0.995497 0.000200374 0.0975824 0.298927 -0.00199023 C -0.00154479 -0.0998212 -1.095118 -0.996008 -1.194688 -1.591714 \" transform=\"matrix(-0.44722,0.89442,0.89442,0.44722,28.66281,27.73743)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.3985;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M 2.610812 -5.219281 L 11.13425 -22.270063 \" transform=\"matrix(1,0,0,-1,40.022,5.019)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.31879;stroke-linecap:round;stroke-linejoin:round;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -1.193907 1.59314 C -1.09648 0.996331 -0.000319481 0.100231 0.297212 -0.000692372 C -0.000323046 -0.0998649 -1.096515 -0.995926 -1.195704 -1.594471 \" transform=\"matrix(0.44902,0.898,0.898,-0.44902,51.15623,27.28748)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.3985;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -16.877469 -33.566938 L -25.186063 -49.606 \" transform=\"matrix(1,0,0,-1,40.022,5.019)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.31879;stroke-linecap:round;stroke-linejoin:round;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -1.19593 1.594892 C -1.096851 0.997229 -0.000851156 0.10031 0.30009 0.00185268 C 0.00200363 -0.0999212 -1.094544 -0.996753 -1.197487 -1.593813 \" transform=\"matrix(-0.4614,0.89082,0.89082,0.4614,14.83603,54.62635)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.3985;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -11.471219 -33.566938 L -3.158719 -49.613813 \" transform=\"matrix(1,0,0,-1,40.022,5.019)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.31879;stroke-linecap:round;stroke-linejoin:round;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -1.195166 1.592032 C -1.095697 0.996768 0.000812022 0.0999195 0.298892 -0.00186084 C -0.00204804 -0.100309 -1.098056 -0.997187 -1.19715 -1.594839 \" transform=\"matrix(0.46143,0.89082,0.89082,-0.46143,36.86374,54.63132)\"/>\n",
       "<g style=\"fill:rgb(0%,0%,0%);fill-opacity:1;\">\n",
       "  <use xlink:href=\"#glyph-1506981941267956-0-1\" x=\"37.407\" y=\"6.718\"/>\n",
       "</g>\n",
       "<g style=\"fill:rgb(0%,0%,0%);fill-opacity:1;\">\n",
       "  <use xlink:href=\"#glyph-1506981941267956-0-1\" x=\"23.234\" y=\"35.064\"/>\n",
       "</g>\n",
       "<g style=\"fill:rgb(0%,0%,0%);fill-opacity:1;\">\n",
       "  <use xlink:href=\"#glyph-1506981941267956-0-2\" x=\"3.321\" y=\"64.761\"/>\n",
       "  <use xlink:href=\"#glyph-1506981941267956-0-3\" x=\"8.551386\" y=\"64.761\"/>\n",
       "  <use xlink:href=\"#glyph-1506981941267956-0-4\" x=\"13.781772\" y=\"64.761\"/>\n",
       "</g>\n",
       "<g style=\"fill:rgb(0%,0%,0%);fill-opacity:1;\">\n",
       "  <use xlink:href=\"#glyph-1506981941267956-0-5\" x=\"32.686\" y=\"64.756\"/>\n",
       "  <use xlink:href=\"#glyph-1506981941267956-0-3\" x=\"37.916386\" y=\"64.756\"/>\n",
       "  <use xlink:href=\"#glyph-1506981941267956-0-4\" x=\"43.146772\" y=\"64.756\"/>\n",
       "</g>\n",
       "<g style=\"fill:rgb(0%,0%,0%);fill-opacity:1;\">\n",
       "  <use xlink:href=\"#glyph-1506981941267956-0-6\" x=\"51.58\" y=\"35.513\"/>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n",
       "\n"
      ],
      "text/plain": [
       "TreeView.LabelledTree({5, 4} directed simple Int64 graph, Any[:-, :-, 4.0, 1.0, :x])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "srand(4)\n",
    "ind2 = GrammaticalEvolution.select(p.select_method, pop, losses)\n",
    "node2 = GrammaticalEvolution.decode(ind2, grammar, :Real).node\n",
    "display(node2, grammar)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5-element Array{Int64,1}:\n",
       "  2685370321605084886\n",
       " -3702360005862138301\n",
       "  8603170902964801110\n",
       "  2744240855864043070\n",
       "  2855546127016599912"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ind1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5-element Array{Int64,1}:\n",
       " -5709378026784038018\n",
       " -4638217506560872160\n",
       "  2412787040955850733\n",
       " -6755966082412089502\n",
       " -6661740532992639830"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ind2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5-element Array{Int64,1}:\n",
       "  2685370321605084886\n",
       " -3702360005862138301\n",
       "  2412787040955850733\n",
       " -6755966082412089502\n",
       " -6661740532992639830"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "srand(4)\n",
    "child = GrammaticalEvolution.crossover(ind1, ind2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       ":x"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "srand(0)\n",
    "ind3 = GrammaticalEvolution.select(p.select_method, pop, losses)\n",
    "node3 = GrammaticalEvolution.decode(ind3, grammar, :Real).node\n",
    "display(node3, grammar)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5-element Array{Int64,1}:\n",
       "  2685370321605084886\n",
       " -3702360005862138301\n",
       "  8603170902964801110\n",
       "  2744240855864043070\n",
       "  2855546127016599912"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ind3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5-element Array{Int64,1}:\n",
       "  2685370321605084886\n",
       " -3702360005862138301\n",
       " -5269856566257922209\n",
       "  2744240855864043070\n",
       "  2855546127016599912"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "srand(5)\n",
    "child = GrammaticalEvolution.mutation(GrammaticalEvolution.RandomMutation(0.5),ind3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8-element Array{Int64,1}:\n",
       "  2685370321605084886\n",
       " -3702360005862138301\n",
       "  8603170902964801110\n",
       "  2744240855864043070\n",
       "  2855546127016599912\n",
       "  2685370321605084886\n",
       " -3702360005862138301\n",
       "  8603170902964801110"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "srand(2)\n",
    "child = GrammaticalEvolution.mutation(GrammaticalEvolution.GeneDuplication(), ind3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5-element Array{Int64,1}:\n",
       "  2685370321605084886\n",
       " -3702360005862138301\n",
       "  8603170902964801110\n",
       "  2744240855864043070\n",
       "  2855546127016599912"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "GrammaticalEvolution.limit_length!(child, 5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "ExprOptimization.ExprOptResult(3{9,2{9,9}}, 57195.99699999999, :(5.0 + 5.0 * 5.0), nothing)"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "optimize(p, grammar, :Real, loss)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "ExprOptimization.ExprOptResult(3{2{1,3{2{1,7}6}}5}, 1.318691936641718e-27, :(x * (x * 3.0 + 2.0) + 1.0), nothing)"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p = GrammaticalEvolutionParams(grammar,:Real,1000,25,10,6,0.2, 0.4, 0.4; select_method=GrammaticalEvolution.TruncationSelection(300))\n",
    "result = optimize(p, grammar, :Real, loss)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Julia 0.6.0",
   "language": "julia",
   "name": "julia-0.6"
  },
  "language_info": {
   "file_extension": ".jl",
   "mimetype": "application/julia",
   "name": "julia",
   "version": "0.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
